<template>
  <div class="admin">
    <el-card class="!border-none" shadow="never">
      <el-form class="mb-[-16px]" :model="formData" inline>
        <el-form-item label="所属企业">
          <el-select class="w-[280px]" v-model="formData.ac_id">
            <el-option label="全部" value="" />
            <el-option v-for="(item, index) in optionsData.companyList" :key="index" :label="item.c_name"
              :value="item.ac_id" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="formData.name" class="w-[280px]" clearable placeholder="机器人名称" @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select class="w-[280px]" v-model="formData.disable">
            <el-option label="全部" value="" />
            <el-option label="禁用" value="1" />
            <el-option label="开启" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
      <el-button v-perms="['agent.agent/add']" type="primary" @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
      <div class="mt-4">
        <el-table :data="pager.lists" size="large">
          <el-table-column label="ID" prop="agent_id" min-width="60" />
          <el-table-column label="头像" prop="phone" min-width="100">
              <template #default="{ row }">
                  <el-avatar :src="row.photo" :size="38" />
              </template>
          </el-table-column>
          <el-table-column label="角色名称" prop="name" min-width="140" />
          <el-table-column label="所属企业" prop="company_name" min-width="260" />

          
          <el-table-column label="APPID" prop="appid" min-width="220" />
          <el-table-column label="KEY" prop="app_key" min-width="220" />
          <el-table-column label="到期时间" prop="create_time" min-width="200" />
          <el-table-column label="套餐余量" prop="disable" min-width="100" />
          <el-table-column label="状态" prop="disable_desc" min-width="100" show-tooltip-when-overflow>
            <template #default="{ row }">
              <el-tag v-if="row.disable == 0" class="ml-2" type="success">开启</el-tag>
              <el-tag v-else class="ml-2" type="danger">关闭</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建人" prop="admin_name" min-width="100" />
          <el-table-column label="创建时间" prop="create_time" min-width="200" show-tooltip-when-overflow />
          <el-table-column label="描述" prop="describe" min-width="100" />
          <el-table-column label="链接" prop="disable_desc" min-width="100" show-tooltip-when-overflow>
            <template #default="{ row }">
              <el-link type="primary" @click="copyText(row.agent_url)">复制链接</el-link>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="340" fixed="right">
            <template #default="{ row }">
              <el-button
                  v-perms="['company.company/edit']"
                  type="primary"
                  link
                  @click="handleEdit(row)"
              >
                  套餐订阅
              </el-button>
              <el-button
                  v-perms="['company.company/edit']"
                  type="primary"
                  link
                  @click="handleEdit(row)"
              >
                  额度充值
              </el-button>
              <el-button type="primary" link @click="copyText(row.agent_url)">
                复制链接
              </el-button>
              <el-button v-perms="['agent.agent/edit']" type="primary" link @click="handleEdit(row)">
                编辑
              </el-button>
              <el-button v-if="row.root != 1" v-perms="['agent.agent/delete']" type="danger" link
                @click="handleDelete(row.agent_id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
  </div>
</template>

<script lang="ts" setup name="admin">
import {
  adminEdit,
  adminLists,
  adminDelete,
  adminUpdStatus,
} from "@/api/robot";
import { companyListAll } from "@/api/company";
import { useDictOptions } from "@/hooks/useDictOptions";
import { usePaging } from "@/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 表单数据
const formData = reactive({
  ac_id: "",
  name: "",
  disable: "",
  type: '2'
});
const showEdit = ref(false);
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: adminLists,
  params: formData,
});

const changeStatus = (data: any) => {
  adminUpdStatus({
    id: data.id,
    disable: data.disable,
  }).finally(() => {
    getLists();
  });
  adminEdit({
    id: data.id,
    account: data.account,
    name: data.name,
    role_id: data.role_id,
    disable: data.disable,
    multipoint_login: data.multipoint_login,
  }).finally(() => {
    getLists();
  });
};
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

const handleDelete = async (agent_id: number) => {
  await feedback.confirm("确定要删除？");
  await adminDelete({ agent_id });
  getLists();
};

const copyText = async (url: string) => {
  // 使用navigator.clipboard.writeText复制文本到剪贴板
  navigator.clipboard
    .writeText(url)
    .then(() => {
      // 复制成功的处理     
      feedback.msgSuccess("复制成功")
    })
    .catch((err) => {
      // 复制失败的处理
      feedback.msgError("复制失败")
    });
};

const { optionsData } = useDictOptions<{
  companyList: any[];
}>({
  companyList: {
    api: companyListAll,
  },
});

onMounted(() => {
  getLists();
});
</script>